//
//  UIImage+ImageName.m
//  ChinaInsight
//
//  Created by liang on 13-12-6.
//  Copyright (c) 2013年 com.eliteworkltd. All rights reserved.
//

#import "UIImage+ImageName.h"

@implementation UIImage (UIImage_ImageName)

+(UIImage *)imageNameWithString:(NSString *)name
{
    NSString *path = [Utils getBundlePath:name];
    return [UIImage imageWithContentsOfFile:path];
}

+ (__autoreleasing UIImage *)imageInBundle:(NSString *)imgName {
    
	CGFloat s = 1.0f;
	if([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
		s = [[UIScreen mainScreen] scale];
	}
	
    NSString *extenstion = [imgName pathExtension];
	NSString *path = nil;
    if ([[extenstion lowercaseString] isEqualToString:@"png"]) {
        
        if ( s > 1 ) {
            
            NSMutableString *temp = [NSMutableString stringWithString:imgName];
            [temp replaceOccurrencesOfString:@".png"
                                  withString:@"@2x.png"
                                     options:NSBackwardsSearch
                                       range:NSMakeRange(temp.length - 4, 4)];
            
            path = temp;
            
        } else {
            
            path = imgName;
        }
        
    } else {
        
        path = [NSString stringWithFormat:@"%@%@.png", imgName, s > 1 ? @"@2x" : @""];
    }
    
	return [UIImage imageWithCGImage:
            [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:path]].CGImage
                               scale:s
                         orientation:UIImageOrientationUp];
}

@end
